package com.swq.java.code.chapter20.code11;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class PopupMenuDemo extends JFrame {
    private String[] colorNames = {"Blue", "Yellow", "Red"};
    private Color[] colors = {Color.BLUE, Color.YELLOW, Color.RED};
    private JRadioButtonMenuItem items[];
    private JPopupMenu popupMenu = new JPopupMenu();

    private ActionListener itemHandler = new ActionListener() {  //处理菜单项的事件
        public void actionPerformed(ActionEvent e) {
            for (int i = 0; i < items.length; i++)
                if (e.getSource() == items[i]) {  //判断用户选择了哪个菜单项
                    getContentPane().setBackground(colors[i]);
                    repaint();
                    return;
                }
        }
    };

    public PopupMenuDemo(String title) {
        super(title);

        ButtonGroup colorGroup = new ButtonGroup();
        items = new JRadioButtonMenuItem[3];

        for (int i = 0; i < items.length; i++) {
            items[i] = new JRadioButtonMenuItem(colorNames[i]);
            popupMenu.add(items[i]);
            colorGroup.add(items[i]);
            items[i].addActionListener(itemHandler);
        }

        getContentPane().setBackground(Color.WHITE);

        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                checkForTriggerEvent(e);
            }

            public void mouseReleased(MouseEvent e) {
                checkForTriggerEvent(e);
            }

            private void checkForTriggerEvent(MouseEvent e) {
                if (e.isPopupTrigger())  //弹出菜单
                    popupMenu.show(e.getComponent(), e.getX(), e.getY());
            }
        });

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 200);
        setVisible(true);
    }

    public static void main(String args[]) {
        new PopupMenuDemo("Hello");
    }
}
